﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using TravelSite.DAL;
using TravelSite.DAL.Repositories;
using TravelSite.Entities;
using TravelSite.Code;
using System.Globalization;
using OfficeOpenXml;

namespace TravelSite.Admin.Bookings
{
    public partial class CalendarPage : BaseDataPage
    {

        protected List<Contact> GridData
        {
            get
            {
                if (this.Session["GridData"] != null)
                    return (List<Contact>)this.Session["GridData"];
                else
                    return null;
            }
            set
            {
                this.Session["GridData"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                calendar.VisibleDate = calendar.SelectedDate = DateTime.Today;
                LoadDataFromDb();
                LoadData();
            }
        }

        public override void LoadData()
        {
            
        }

        protected void LoadDataFromDb()
        {
            using (var repo = RepositoryFactory.ResolveRepository<ContactRepository>())
            {
                DateTime startDate = calendar.VisibleDate.AddMonths(-1);
                startDate = new DateTime(startDate.Year, startDate.Month, 1);
                DateTime endDate = calendar.VisibleDate.AddMonths(2);
                endDate = new DateTime(endDate.Year, endDate.Month, 1).AddDays(-1);
                
                GridData = repo.TourList(startDate, endDate).ToList();
                
            }
        }

        protected void calendar_OnDayRender(Object source, DayRenderEventArgs e)
        {
            DateTime dateRender = e.Day.Date;
            List<Contact> data = GridData.Where(x => (x.TourDateDate == dateRender) && (!string.IsNullOrEmpty(x.TourTime))).OrderBy(x => x.FullName).ToList();
            if (data.Any())
            {
                string iconGreen = string.Format("<img src=\"{0}images/bullet_ball_green.png\" alt=\"\" title=\"Confirmed\"/>", ResolveClientUrl("~"));
                string iconYellow = string.Format("<img src=\"{0}images/bullet_ball_yellow.png\" alt=\"\" title=\"Pending\"/>", ResolveClientUrl("~"));
                string iconRed = string.Format("<img src=\"{0}images/bullet_ball_red.png\" alt=\"\" title=\"Canceled\"/>", ResolveClientUrl("~"));
                string tourUrl = "Tour.aspx?date=" + dateRender.ToString("yyyyMMdd");
                HyperLink tourLink = new HyperLink();
                tourLink.NavigateUrl = tourUrl;
                
                for (int i = 0; i < data.Count; ++i)
                {
                    Contact item = data[i];
                    if (i <= 1)
                    {
                        tourLink.Controls.Add(new LiteralControl("<br/>" + item.FullName));
                        if (item.IsConfirmed) tourLink.Controls.Add(new LiteralControl(iconGreen));
                        else if (item.IsCanceled) tourLink.Controls.Add(new LiteralControl(iconRed));
                        else if (item.IsPending) tourLink.Controls.Add(new LiteralControl(iconYellow));
                    }
                    else if (i == 2)
                    {
                        tourLink.Controls.Add(new LiteralControl("<br/>Total " + data.Count.ToString() + " events"));
                        break;
                    }
                }
                e.Cell.Controls.Add(tourLink);
            }

        }

        protected void calendar_OnVisibleMonthChanged(Object sender, MonthChangedEventArgs e)
        {
            LoadDataFromDb();
        }

        
    }
}